package com.scm.biz.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.scm.biz.bean.Customer;
import com.scm.biz.query.customer.CustomerQuery;
import com.scm.biz.result.CustomerResult;
import com.scm.biz.service.BusinessConstantService;
import com.scm.biz.service.BusinessFileService;
import com.scm.biz.service.CustomerContactService;
import com.scm.biz.service.CustomerService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.core.bean.resp.RespBean;
import com.scm.utils.Const;

@Controller
@RequestMapping("/customer")
public class CustomerAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	@Resource
	private CustomerService customerService;
	
	@Resource
	private BusinessFileService businessFileService;
	
	@Resource
	private CustomerContactService customerContactService;
	
	@Resource
	private BusinessConstantService businessConstantService;
	
	@RequestMapping("/list")
	public ModelAndView doCustomerlist(CustomerQuery customerQuery,HttpServletRequest req){
		if(customerQuery.getPageNo()==null||customerQuery.getPageNo()==0) customerQuery.setPageNo(1);
		if(customerQuery.getFullname()!=null&&!customerQuery.getFullname().trim().isEmpty()){
			customerQuery.setFullname(customerQuery.getFullname().trim());
			customerQuery.setAbbreviation(customerQuery.getFullname());
		}
		List<CustomerResult> list=customerService.getList(customerQuery);
		//获取查询总记录数
		int queryTotal=customerService.getTotalByQuery(customerQuery);
		customerQuery.setRecourdCount(queryTotal);
		int total=customerService.getTotal();
		//签约客户
		int signedCustomers=customerService.getSignedCustomerCount();
		ModelAndView mav=new ModelAndView("customer/customer_list");
		mav.addObject("customerList", list);
		mav.addObject("splitPage", customerQuery.getSplitPage());
		mav.addObject("query",customerQuery);
		//签约客户数
		mav.addObject("signedCustomers", signedCustomers);
		//未签约客户数
		mav.addObject("potentialCustomers", total-signedCustomers);
		mav.addObject("path", Const.getPath(req));
		return mav;
	}
	
	/**
	 * 跳转到增加客户页面
	 * @return 
	 */
	@RequestMapping("/doShowCustomerAddPage")
	public ModelAndView doShowCustomerAddPage(){
		return new ModelAndView("customer/customer_add");
	}
	
	/**
	 * 增加客户，并跳转到客户列表页
	 * @param customer 新增客户类
	 * @return 
	 * @throws UfdmException 
	 * @throws Exception
	 */
	@RequestMapping("/doAddCustomer")
	@ResponseBody
	public RespBean<String> doAddCustomer(MultipartHttpServletRequest req,Customer customer) throws UfdmException{
		if(customer.getFullname()==null||customer.getFullname().trim().isEmpty()){
			throw new UfdmException("客户名为空");
		}
		int count=customerService.getByFullname(null,customer.getFullname().trim());
		if(count!=0){
			throw new UfdmException("客户名已存在");
		}
		if(customer.getAbbreviation()!=null&&!customer.getAbbreviation().trim().isEmpty()){
			count=customerService.getByAbbreviation(null,customer.getAbbreviation().trim());
			if(count!=0){
				throw new UfdmException("客户简称已存在");
			}
		}
		if(customer.getCategory()==null||customer.getCategory().trim().isEmpty()){
			throw new UfdmException("客户未分类");
		}
		if(customer.getLevel()==null||customer.getLevel().trim().isEmpty()){
			throw new UfdmException("客户未分级");
		}
		RespBean<String> respBean=new RespBean<String>();
		customer.setCreator((Integer)BaseAction.doGetSession("userId"));
		customerService.save(req,customer);
		respBean.setBody(customer.getId()+"");
		return respBean;
	}
	
	/**
	 * 查出客户信息，返回编辑页面和数据
	 * @param id 客户ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/doShowCustomerEditPage")
	public ModelAndView doShowCustomerEditPage(int id){
		CustomerResult customer=customerService.getById(id);
		ModelAndView mav=new ModelAndView();
		mav.addObject("customer",customer);
		mav.setViewName("customer/customer_edit");
		return mav;
	}
	
	/**
	 * 更新客户信息
	 * @param attach 附件
	 * @param customer 客户类
	 * @param req 
	 * @return
	 * @throws UfdmException 
	 * @throws Exception 
	 */
	@RequestMapping("/doEditCustomer")
	@ResponseBody
	public RespBean<String> doEditCustomer(MultipartHttpServletRequest req,Customer customer) throws UfdmException{
		if(customer.getFullname()!=null&&!customer.getFullname().trim().isEmpty()){
			int count=customerService.getByFullname(customer.getId(),customer.getFullname().trim());
			if(count!=0){
				throw new UfdmException("客户名已存在");
			}
		}
		if(customer.getAbbreviation()!=null&&!customer.getAbbreviation().trim().isEmpty()){
			int count=customerService.getByAbbreviation(customer.getId(),customer.getAbbreviation().trim());
			if(count!=0){
				throw new UfdmException("客户简称已存在");
			}
		}
		RespBean<String> respBean=new RespBean<String>();
		customerService.modifyById(req,customer);
		return respBean;
	}
	
	/**
	 * 删除客户记录
	 * @param id
	 */
	@RequestMapping("/doRemoveCustomer")
	@ResponseBody
	public RespBean<Boolean> doRemoveCustomer(int id){
		RespBean<Boolean> respBean=new RespBean<Boolean>();
		customerService.removeById(id);
		respBean.setBody(true);
		return respBean;
	}
	
	/**
	 * 显示客户详情
	 * @param id 客户ID
	 * @return
	 */
	@RequestMapping("/doShowCustomerDetailPage")
	public ModelAndView doShowCustomerDetailPage(Integer id){
		CustomerResult customer=customerService.getById(id);
		ModelAndView mav=new ModelAndView();
		mav.addObject("customer",customer);
		mav.setViewName("customer/customer_detail");
		return mav;
	}
	
	/**
	 * 验证客户名称是否重名
	 * @param fullname 客户名
	 * @return
	 * @throws UfdmException 
	 */
	@RequestMapping("/doCheckFullname")
	@ResponseBody
	public RespBean<Boolean> doCheckCustomerName(Long id,String fullname) throws UfdmException{
		RespBean<Boolean> respBean=new RespBean<Boolean>();
		if(fullname==null||fullname.trim().isEmpty()){
			throw new UfdmException("客户名为空");
		}
		int count=customerService.getByFullname(id,fullname.trim());
		if(count!=0){
			respBean.setBody(true);
		}else{
			respBean.setBody(false);
		}
		return respBean;
	}
	
	/**
	 * 验证客户简称是否存在
	 * @param abbreviation
	 * @return true 已存在
	 */
	@RequestMapping("/doCheckAbbreviation")
	@ResponseBody
	public RespBean<Boolean> doCheckAbbreviation(Long id,String abbreviation){
		RespBean<Boolean> respBean=new RespBean<Boolean>();
		if(abbreviation==null||abbreviation.trim().isEmpty()){
			respBean.setBody(false);
			return respBean;
		}
		int count=customerService.getByAbbreviation(id,abbreviation.trim());
		if(count!=0){
			respBean.setBody(true);
		}else{
			respBean.setBody(false);
		}
		return respBean;
	}
	
	
	
	@RequestMapping("/doGetAllCustomer")
	@ResponseBody
	public RespBean<List<Map<String,Object>>> doGetAllCustomer(){
		RespBean<List<Map<String,Object>>> respBean=new RespBean<List<Map<String,Object>>>();
		List<Map<String,Object>> list=customerService.getAllCustomer();
		respBean.setBody(list);
		return respBean;
	}
	
	@RequestMapping("/doGetCustomer")
	@ResponseBody
	public RespBean<List<Map<String,Object>>> doGetFullname(String name){
		RespBean<List<Map<String,Object>>> respBean=new RespBean<List<Map<String,Object>>>();
		List<Map<String,Object>> list=customerService.getCustomerByName(name);
		respBean.setBody(list);
		return respBean;
	}
	
	
}
